package com.gientech.vuedemo.controller;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.gientech.vuedemo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
public class LoginController {


    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/login")
    public String login(@RequestBody User user){

        List<String> userlist = stringRedisTemplate.opsForList().range("admin",0,-1);

        for (String s : userlist) {
            User user1 = JSONUtil.toBean(s, User.class);
            if (user.getName().equals(user1.getName())&&user.getPassword().equals(user1.getPassword())){
                return "200,登录成功";
            }
        }

        return "800,用户名密码错误";
    }

    @RequestMapping("/register")
    public String register(@RequestBody User user){
        Long i = stringRedisTemplate.opsForList().rightPush("user", JSONUtil.toJsonStr(user));
        System.out.println(i);
        if (i == null || i < 1){
            return "700,注册失败";
        }
        return "200,注册成功";
    }

    @RequestMapping("/getuser")
    public String getuser(){
        List<String> list = stringRedisTemplate.opsForList().range("user", 0, -1);

        return ""+list;
    }

}
